package servlets.site;

import gestionnaires.GestionnaireDeCategorieAge;
import gestionnaires.GestionnaireDeFilms;
import utilisateurs.modeles.CategorieAge;
import utilisateurs.modeles.Film;
import helpers.ServletHelper;
import java.io.IOException;
import java.util.Collection;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import views.AgeRatingLightView;

/*
 * Cette servlet contient tout les traitements pour les restrictions d'age sur les jeux
 */
@WebServlet(name = "AgeRatingServlet", urlPatterns = {"/AgeRatingServlet"})
public class AgeRatingServlet extends HttpServlet {

    ///////////////////////////// GESTIONNAIRES ////////////////////////////
    @EJB
    private GestionnaireDeCategorieAge ageRatingFacade;
    @EJB
    private GestionnaireDeFilms jeuVideoFacade;
    ////////////////////////////////////////////////////////////////////

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        int id = ServletHelper.getIntegerValue(request.getParameter("id"));
        String start = request.getParameter("start");
        int totalPages = 0;
        String forwardTo = "";

        // Si l'id de la categorie est valide,
        if (id > 0) {
            CategorieAge ageRating = ageRatingFacade.find(id);

            if (ageRating != null) {
                // récupération des parametres de tri
                String sort = ServletHelper.getValue(request.getParameter("sort"), "releaseDate");
                String value = ServletHelper.getValue(request.getParameter("value"), "desc");
                // on calcul le nombre de pages nécessaires à l'affichage des jeux de la catégorie d'age
                totalPages = ServletHelper.getTotalPages(jeuVideoFacade.countByAgeRating(ageRating), ServletHelper.JEUXVIDEO_PAR_PAGE);
                int debut = ServletHelper.getStart(start, totalPages);
                // récupration des jeux videos et modifications de la variable de session
                Collection<Film> jeuxVideo = jeuVideoFacade.getByAgeRating(ageRating, debut * ServletHelper.JEUXVIDEO_PAR_PAGE, ServletHelper.JEUXVIDEO_PAR_PAGE, sort, value);
                request.setAttribute("ageRatingLV", new AgeRatingLightView(ageRating, jeuxVideo));
                request.setAttribute("totalPages", totalPages);
            } else {
                // sinon, affichage de la page d'erreur
                forwardTo = "?page=error";
            }
        // sinon on envoie dans la session toutes les categories.
        } else {
            request.setAttribute("ageRatings", ageRatingFacade.getAll());
        }
        // redirection
        RequestDispatcher dispatcher = request.getRequestDispatcher("home.jsp" + forwardTo);
        dispatcher.forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
